<template>
  <div class="fav-list">
    <div class="list-header">
      <div class="total-count">共 {{ list.length }} 条收藏</div>
    </div>

    <div class="fav-items">
      <div
        class="fav-item"
        v-for="item in list"
        :key="item.itemId"
        @click="goToDetail(item)"
      >
        <div class="content-wrapper">
          <div class="text-content">
            <h3 class="title">{{ item.title }}</h3>
            <div class="meta">
              <span class="author">{{ item.authorName }}</span>
              <span class="comments">🗨️ {{ item.commentCount }}</span>
            </div>
          </div>
          <div class="cover" v-if="item.cover">
            <img :src="item.cover" alt="封面" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getFav } from '@/apis/fav' // 导入接口方法

const router = useRouter()

// 本地测试数据
const list = ref([])
const fetchFavList = async () => {
  try {
    const res = await getFav()
    if (res.code === '200') {
      list.value = res.data
    } else {
      console.error('获取收藏失败:', res.message)
    }
  } catch (error) {
    console.error('接口请求异常:', error)
    ElMessage.error('获取收藏列表失败')
  }
}

const goToDetail = (item) => {
  switch (item.itemType) {
    case 1:
      router.push(`/home/dynamic/${item.itemId}`)
      break
    case 2:
      router.push(`/home/article/${item.itemId}`)
      break
    default:
      console.warn('未知内容类型')
  }
}

onMounted(() => {
  fetchFavList()
})
</script>

<style scoped>
.list-header {
  margin-bottom: 20px;
  padding: 16px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.header-content {
  display: flex;
  justify-content: space-between; /* 左右两端对齐 */
  align-items: center; /* 垂直居中 */
}

.total-count {
  font-size: 14px;
  color: #666;
  margin: 0; /* 移除默认的margin-top */
}

/* 调整按钮样式 */
.el-button {
  margin-left: auto; /* 自动左外边距推至右侧 */
  padding: 8px 10px;
}

/* 新增历史记录容器样式 */
.fav-items {
  display: grid;
  gap: 16px;
  max-height: 70vh;
  height: 600px;
  overflow-y: auto;
  padding-right: 8px;

  /* 新增关键属性 */
  grid-auto-rows: 120px; /* 设置每行固定高度 */
  align-content: start; /* 内容顶部对齐 */
}

/* 修改收藏项样式 */
.fav-item {
  background: white;
  padding: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  transition: all 0.2s;

  /* 确保高度固定 */
  height: 100px;
  min-height: 100px;
  max-height: 100px;

  /* 防止内容溢出 */
  overflow: hidden;
}

/* 调整内容容器 */
.content-wrapper {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  height: 100%; /* 继承父容器高度 */
}

.fav-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.item-type {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.text-content {
  flex: 1;
}

.title {
  margin: 0;
  font-size: 16px;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #666;
  margin-top: 8px;
  height: 20px; /* 固定元信息高度 */
}

.cover img {
  width: 100px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
}
</style>
